﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Facebook;
using FacebookClient;
using FacebookClient.Database;

namespace CommentDownloader
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        #region Variables
        private Client facebookClient = null;
        private FacebookDatabaseDataContext fbDataContext = null;
        #endregion

        #region Form Events
        private void lnkLoginLogout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (facebookClient == null)
            {
                Thread login = new Thread(new ThreadStart(Login));
                login.SetApartmentState(ApartmentState.STA);
                login.Start();
            }
            else
            {
            }
        }

        private void lnkLoginAsDifferentUser_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Thread login = new Thread(new ThreadStart(LoginAsDifferentUser));
            login.SetApartmentState(ApartmentState.STA);
            login.Start();
        }
        #endregion

        #region Private Methods
        private void Login()
        {
            facebookClient = new Client(true);
            Invoke(new UpdateUserDelegate(UpdateUser), facebookClient.Me);
        }

        private void LoginAsDifferentUser()
        {
            facebookClient = new Client(false);
            Invoke(new UpdateUserDelegate(UpdateUser), facebookClient.Me);
        }

        private delegate void UpdateUserDelegate(User user);
        private void UpdateUser(User user)
        {
            if (user != null)
            {
                lbWelcome.Text = "Welcome, " + user.Name + "!";
                lnkLoginLogout.Text = "(Logout)";
                lnkLoginAsDifferentUser.Visible = false;
            }
            else
            {
                lbWelcome.Text = "";
                lnkLoginLogout.Text = "Login";
                lnkLoginAsDifferentUser.Visible = true;
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            fbDataContext = new FacebookDatabaseDataContext();
            var users = fbDataContext.Users.ToList();
            cbbUser.DisplayMember = "Name";
            cbbUser.ValueMember = "Id";
            cbbUser.DataSource = users;
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            var to = ((int) DateTimeConvertor.ToUnixTime(dtpToTime.Value.Date)).ToString();
            var from = ((int) DateTimeConvertor.ToUnixTime(dtpFromTime.Value.Date)).ToString();
            var comments = fbDataContext.Comments.Where(c => c.OwnerUserId == (long)cbbUser.SelectedValue
                && c.Time.CompareTo(to) <= 0 && c.Time.CompareTo(from) >= 0).Select(c => c.Content);
            foreach (var content in comments)
            {
                if (!string.IsNullOrWhiteSpace(content.ToString()))
                    txtComments.Text += content.ToString() + "\r\n";
            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            facebookClient.FetchFriends();
            facebookClient.FetchComments();
        }
    }
}
